using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ControllsModifire : SynchronizedGameObject
{
	private CharacterController charControl;

	private ThirdPersonController moveControl;

	private float cXSpeed;

	private float cYSpeed;

	private bool _onStair;

	public float maxYspeed = 6f;

	public float ySpeed = 4f;

	public float yDamping = 0.6f;

	public float maxYthreshold = 0.1f;

	public float maxXspeed = 6f;

	public float xSpeed = 4f;

	public float xDamping = 0.9f;

	public float maxXthreshold = 0.1f;

	[Tooltip("На каком расстоянии от точки персонаж будет перемещен к ней")]
	public float onStairCatchRange = 4f;

	[Tooltip("Угол между персонажем и лесницей на котором лесницей воспользуются")]
	public float activeAngle = 15f;

	[Tooltip("Контроль по Х")]
	public bool canControlX = true;

	[Tooltip("Контроль по У")]
	public bool canControlY = true;

	public float leapDuration = 0.2f;

	public string enterPointName = "EnterPoint";

	public string exitPointName = "ExitPoint";

	private Tweener animationPosition;

	private Tweener animationRotation;

	private PlayerBehavior behavior;

	private float baseGravity;

	public bool onStair
	{
		get
		{
			return _onStair;
		}
	}

	public override void Start()
	{
		if (SceneManager.GetActiveScene().name.Equals("Lobby"))
		{
			Object.Destroy(this);
			return;
		}
		charControl = GetComponent<CharacterController>();
		moveControl = GetComponent<ThirdPersonController>();
		behavior = GetComponent<PlayerBehavior>();
	}

	public void Breack()
	{
		if (animationPosition != null)
		{
			HOTween.Kill(animationPosition);
		}
		if (animationRotation != null)
		{
			HOTween.Kill(animationRotation);
		}
		animationPosition = null;
		animationRotation = null;
		EnableMoveControllOnRespawn();
	}

	public void OnStair(bool onStair, GameObject stair = null)
	{
		if (behavior == null || behavior.onLadder == onStair)
		{
			return;
		}
		if (base.isMine && !settings.offlineMode)
		{
			PhotonNetwork.RPC(base.photonView, "SitInOnline", PhotonTargets.Others, false, (!onStair) ? string.Empty : stair.name);
		}
		base.transform.parent = ((!(stair == null) && onStair) ? stair.transform : null);
		behavior.onLadder = onStair;
		if (behavior.isMine)
		{
			GameController.instance.useLestncu = onStair;
		}
		_onStair = onStair;
		cYSpeed = 0f;
		cXSpeed = 0f;
		moveControl.verticalSpeed = 0f;
		Transform neerChild = base.transform;
		if (onStair)
		{
			DisableMoveControl(true);
			if (stair != null)
			{
				neerChild = GetNeerChild(stair.transform, enterPointName);
				if (!(onStairCatchRange < 0f) && !(Vector3.Distance(base.transform.position, neerChild.position) < onStairCatchRange))
				{
					if (base.transform.position.y > stair.transform.position.y)
					{
						base.transform.position -= Vector3.up * 0.3f;
					}
					else
					{
						base.transform.position += Vector3.up * 0.3f;
					}
					moveControl.isMoving = false;
					base.transform.rotation = neerChild.rotation;
				}
			}
			if (behavior.isMine && (onStairCatchRange < 0f || Vector3.Distance(base.transform.position, neerChild.position) < onStairCatchRange))
			{
				if (animationPosition != null)
				{
					HOTween.Kill(animationPosition);
				}
				if (animationRotation != null)
				{
					HOTween.Kill(animationRotation);
				}
				animationPosition = HOTween.To(base.transform, leapDuration, new TweenParms().Prop("position", neerChild.position).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					animationPosition = null;
				}));
				animationRotation = HOTween.To(base.transform, leapDuration, new TweenParms().Prop("rotation", neerChild.rotation).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					animationRotation = null;
				}));
			}
			else if (!onStair)
			{
				animationPosition = null;
				animationRotation = null;
			}
			return;
		}
		if (stair != null)
		{
			neerChild = GetNeerChild(stair.transform, exitPointName);
		}
		if (neerChild != null && Vector3.Distance(base.transform.position, neerChild.position) < onStairCatchRange)
		{
			animationPosition = HOTween.To(base.transform, leapDuration, new TweenParms().Prop("position", neerChild.position).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				animationPosition = null;
			}));
			animationRotation = HOTween.To(base.transform, leapDuration, new TweenParms().Prop("rotation", neerChild.rotation).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				animationRotation = null;
			}));
		}
		DisableMoveControl(false);
	}

	private Transform GetNeerChild(Transform perant, string objName = "")
	{
		Transform transform = null;
		foreach (Transform item in perant)
		{
			if (!(objName != string.Empty) || !(item.name != objName))
			{
				if (transform == null)
				{
					transform = item;
				}
				else if (Vector3.Distance(item.position, base.transform.position) < Vector3.Distance(transform.position, base.transform.position))
				{
					transform = item;
				}
			}
		}
		return transform;
	}

	private void StairUpdate()
	{
		if (behavior == null)
		{
			return;
		}
		if (behavior.isGrounded)
		{
			OnStair(false);
		}
		else
		{
			if (!behavior.isMine)
			{
				return;
			}
			if (canControlY)
			{
				if (Mathf.Abs(moveControl.joyScript.position.y) > maxYthreshold)
				{
					if (maxYspeed > cYSpeed)
					{
						cYSpeed += moveControl.joyScript.position.y * ySpeed * Time.deltaTime;
					}
				}
				else if (cYSpeed > 0.01f || cYSpeed < -0.01f)
				{
					cYSpeed -= cYSpeed * yDamping * Time.deltaTime;
				}
				else
				{
					cYSpeed = 0f;
				}
				moveControl.transform.localPosition += new Vector3(0f, cYSpeed, 0f) * Time.deltaTime;
			}
			if (canControlX)
			{
				if (Mathf.Abs(moveControl.joyScript.position.x) > maxXthreshold)
				{
					if (maxXspeed > cXSpeed)
					{
						cXSpeed += moveControl.joyScript.position.x * xSpeed * Time.deltaTime;
					}
				}
				else if (cXSpeed > 0.01f || (double)cXSpeed < -0.01)
				{
					cXSpeed -= cXSpeed * xDamping * Time.deltaTime;
				}
				else
				{
					cXSpeed = 0f;
				}
				moveControl.transform.localPosition += new Vector3(cXSpeed, 0f, 0f) * Time.deltaTime;
			}
		}
		if (moveControl.jumping)
		{
			OnStair(false);
			base.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0f, 0.2f, -1f);
		}
	}

	private void OnTriggerEnter(Collider other)
	{
		if ((bool)behavior && behavior.onLadder)
		{
			return;
		}
		if (moveControl == null)
		{
			charControl = GetComponent<CharacterController>();
			moveControl = GetComponent<ThirdPersonController>();
			behavior = GetComponent<PlayerBehavior>();
		}
		if (charControl.transform.parent != null)
		{
			return;
		}
		float num = Quaternion.Angle(base.transform.rotation, other.transform.rotation);
		if ((!(num < activeAngle) && (!(num > 180f - activeAngle / 2f) || !(num < 180f + activeAngle / 2f))) || !behavior.isMine || !(other.gameObject.tag == "Stair"))
		{
			return;
		}
		foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
		{
			if (listPlayer != PlayerBehavior.MyPlayer && listPlayer.currentLadderName == other.gameObject.name)
			{
				return;
			}
		}
		OnStair(true, other.gameObject);
	}

	private void OnDestroy()
	{
		OnStair(false);
	}

	[PunRPC]
	private void SitInOnline(string name)
	{
		if (name == string.Empty)
		{
			base.transform.parent = null;
		}
		else
		{
			GameObject gameObject = GameObject.Find(name);
			if (gameObject != null)
			{
				base.transform.SetParent(gameObject.transform);
			}
		}
		if (moveControl == null)
		{
			charControl = GetComponent<CharacterController>();
			moveControl = GetComponent<ThirdPersonController>();
			behavior = GetComponent<PlayerBehavior>();
		}
		behavior.currentLadderName = name;
		behavior.onLadder = name != string.Empty;
	}

	private void OnTriggerExit(Collider other)
	{
		if (other.gameObject.tag == "Stair")
		{
			OnStair(false, other.gameObject);
		}
	}

	public void DisableMoveControl(bool disable)
	{
		if (!(behavior == null))
		{
			if (disable)
			{
				baseGravity = moveControl.gravity;
				moveControl.gravity = 0f;
				moveControl.inAirVelocity = Vector3.zero;
				moveControl.moveSpeed = 0f;
				moveControl.verticalSpeed = 0f;
				moveControl.jumping = false;
			}
			else
			{
				moveControl.gravity = 20f;
			}
			if (behavior.isMine)
			{
				GameController.instance.curScriptLestnica = this;
			}
		}
	}

	public void EnableMoveControllOnRespawn()
	{
		if (behavior == null)
		{
			return;
		}
		if (behavior.isMine)
		{
			GameController.instance.curScriptLestnica = this;
		}
		if (!(behavior == null) && behavior.onLadder)
		{
			if (base.isMine && !settings.offlineMode)
			{
				PhotonNetwork.RPC(base.photonView, "SitInOnline", PhotonTargets.Others, false, string.Empty);
			}
			behavior.onLadder = false;
			if (behavior.isMine)
			{
				GameController.instance.useLestncu = false;
			}
			_onStair = false;
			cYSpeed = 0f;
			cXSpeed = 0f;
			moveControl.verticalSpeed = 0f;
		}
	}

	private void Update()
	{
		if (animationPosition == null && animationRotation == null && onStair)
		{
			StairUpdate();
		}
	}
}
